package com.itcat.controller;

import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.pojo.UmsMenu;
import com.itcat.mysql.pojo.UmsResource;
import com.itcat.mysql.pojo.UmsRole;
import com.itcat.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * [用户权限管理]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/2 12:37]
 */
@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;


    /***
     * 获取所有角色
     * @author Cr Mr
     * @date 2022/4/2 12:40
     */
    @GetMapping("listAll")
    public CommonResult listAll() {
        List<UmsRole> list = umsRoleService.listAll();
        return CommonResult.success(list);
    }

    /***
     * 根据角色名称分页获取角色列表
     * @author Cr Mr
     * @date 2022/4/3 0:39
     */
    @GetMapping("list")
    public CommonResult list(@RequestParam(required = false) Integer pageNum,
                             @RequestParam(required = false) Integer pageSize,
                             @RequestParam(required = false) String keyword) {
        CommonPage<UmsRole> page = umsRoleService.getPage(keyword, pageNum, pageSize);
        return CommonResult.success(page);
    }

    /***
     * 获取角色相关菜单
     * @author Cr Mr
     * @date 2022/4/3 8:39
     */
    @GetMapping("listMenu/{roleId}")
    public CommonResult listMenu(@PathVariable Long roleId) {
        List<UmsMenu> menuList = umsRoleService.listMenu(roleId);
        return CommonResult.success(menuList);
    }

    /***
     * 给角色分配菜单
     * @author Cr Mr
     * @date 2022/4/3 10:29
     */
    @PostMapping("allocMenu")
    public CommonResult allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        umsRoleService.allocMenu(roleId, menuIds);
        return CommonResult.success();
    }

    /***
     * 添加角色
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @PostMapping("create")
    public CommonResult create(@RequestBody UmsRole umsRole) {
        umsRoleService.create(umsRole);
        return CommonResult.success();
    }

    /***
     * 给角色分配资源
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @PostMapping("allocResource")
    public CommonResult allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        umsRoleService.allocResource(roleId, resourceIds);
        return CommonResult.success();
    }

    /***
     * 批量删除角色
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @PostMapping("delete")
    public CommonResult delete(@RequestParam List<Long> ids) {
        umsRoleService.delete(ids);
        return CommonResult.success();
    }

    /***
     * 获取角色相关资源
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @GetMapping("listResource/{roleId}")
    public CommonResult listResource(@PathVariable Long roleId) {
        List<UmsResource> list = umsRoleService.listResource(roleId);
        return CommonResult.success(list);
    }

    /***
     * 修改角色
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @PostMapping("update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsRole umsRole) {
        umsRoleService.update(id, umsRole);
        return CommonResult.success();
    }

    /***
     * 修改角色状态
     * @author Cr Mr
     * @date 2022/4/3 10:33
     */
    @PostMapping("updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        umsRoleService.updateStatus(id, status);
        return CommonResult.success();
    }


}
